package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        log.info("查询今日运营数据");

        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        Integer orderCount = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(map);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (orderCount != 0 && validOrderCount != 0) {
            //计算订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }
        //计算新增用户数
        Integer newUsers = userMapper.countByMap(map);


        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }
    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders(){
        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);

        Map map = new HashMap();
        map.put("begin", begin);
//        订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     *  菜品统计数据
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes(){
        Integer sold = dishMapper.countStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countStatus(StatusConstant.DISABLE);
        return new DishOverViewVO().builder()
                .sold( sold)
                .discontinued( discontinued)
                .build();
    }

    /**
     * 套餐统计数据
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals(){
        Integer sold = setmealMapper.countStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.countStatus(StatusConstant.DISABLE);
        return new SetmealOverViewVO().builder()
                .sold( sold)
                .discontinued( discontinued)
                .build();

    }
}
